import threading
import uiautomator2 as u2


class MessageThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.message_list = []
        self.size = 0
        self.flag = False

    def append(self, window):
        while self.flag:
            els = window(className='android.widget.RelativeLayout', index=1)
            for e in els:
                if e.child(className='android.widget.TextView').info['clickable']:
                    self.message_list.append(e.child(className='android.widget.TextView').get_text())
            self.message_list = sorted(list(set(self.message_list)), key=self.message_list.index)
            self.size = len(self.message_list)

    def get_message_list(self):
        return self.message_list
